// vuex 的actions数据
import {
  reqAddress,
  reqCategorys,
  reqLogout,
  reqShops,
  reqUser,
  reqShopInfo,
  reqShopRatings,
  reqShopGoods
} from '../api/index'

import {
  RECEIVE_ADDRESS,
  RECEIVE_CATEGORYS,
  RECEIVE_SHOPS,
  RECEIVE_USER_INFO,
  RESET_USER_INFO,
  RECEIVE_GOODS,
  RECEIVE_RATINGS,
  RECEIVE_INFO,
  INCREMENT_FOOD_COUNT,
  DECREMENT_FOOD_COUNT
} from './mutation-types'
import Vue from 'vue'



export default {
  //异步获取地址
  async getAddress(context) {
      
    let geohash = context.state.latitude + ',' + context.state.longitude
    const result = await reqAddress(geohash)
    if (result.code === 0) {
      let obj = {
        address: result.data
      }
      context.commit(RECEIVE_ADDRESS, obj)
    }
  },


  async getCategroys(context) {
    const result = await reqCategorys()
    if (result.code === 0) {
      let obj = {
        categorys: result.data
      }
      context.commit(RECEIVE_CATEGORYS, obj)

    }
  },


  async getShops(context) {
    let aaa = {
      latitude: context.state.latitude,
      longitude: context.state.longitude
    }
    const result = await reqShops(aaa)
    if (result.code === 0) {
      let obj = {
        shops: result.data
      }
      context.commit(RECEIVE_SHOPS, obj)

    }
  },


// 异步获取用户信息
async  getUserInfo({commit}){
let result =await reqUser()
console.log(111)
console.log(result);
if(result.code===0){
  
  commit(RECEIVE_USER_INFO,{userInfo:result.data})
}
},



  // async   getAddress({commit,state}) {

  //     let geohash=state.latitude+','+state.longitude
  //     const result=    await reqAddress(geohash)
  //     if(result.code===0)  commit(RECEIVE_ADDRESS,{address:result.data})

  // },



  //   async  getCategorys({commit}) {
  //     const result=    await reqCategorys()
  //     if(result.code===0)  commit(RECEIVE_CATEGORYS,{categorys:result.data})



  // },

  // async  getShops({commit,state}) {
  //     let{latitude,longitude}={state}
  //     const result=  await reqShops({latitude,longitude})
  //     if(result.code===0)  commit(RECEIVE_SHOPS,{shops:result.data})

  // }


// 记录用户信息
recordUserInfo({commit},{userInfo}){
  commit(RECEIVE_USER_INFO,{userInfo})
  window.localStorage.setItem('userinfo',JSON.stringify(userInfo))
},

// 退出登录
async logout({commit},callback){
  let res=await reqLogout()
  if(res.code==0){
    commit(RESET_USER_INFO)
    callback&&callback()
  }
},


  //获取商家信息 
  async getShopInfo({ commit }) {
    let res = await reqShopInfo()
    if (res.code == 0) {
        commit(RECEIVE_INFO, { info: res.data })


    }
},

//获取商品信息
async getShopGoods({ commit }, callback) {
    let res = await reqShopGoods()
    console.log('goods')
    if (res.code == 0) {
        commit(RECEIVE_GOODS, { goods: res.data })
        callback && callback()


    }
},

//获取评分组件信息
async getShopRatings({ commit }) {
    let res = await reqShopRatings()
    if (res.code == 0) {
        commit(RECEIVE_RATINGS, { ratings: res.data })


    }
},


//购物车方法 触发
updateFoodCount({commit},{isAdd,food,}){
console.log(food);
     if(isAdd){  //如果为true 表示数量加加

       if(!food.count){
        // food.count=1   data更新了 但是视图不会更新
        Vue.set(food,'count',1)
        
        commit(INCREMENT_FOOD_COUNT,{food})
      }else{
         food.count++
       }

     }else{
       if(food.count){
         food.count--
         commit(DECREMENT_FOOD_COUNT,{food})
       }
      
      
     }
}







}
